package com.itheima.reggie.controller;

import com.itheima.reggie.bean.Employee;
import com.itheima.reggie.common.R;
import com.itheima.reggie.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    //PC端用户退出
    @PostMapping("/logout")
    public R logout(HttpSession session) {
        try {
            //将session存储信息清除
            session.invalidate();
            return R.success("退出成功");
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("退出失败");
        }
    }

    //PC端用户登录
    @PostMapping("/login")
    public R login(@RequestBody Employee employee, HttpSession session) {
        //调用Service层
        Employee loginEmployee = employeeService.login(employee);
        //判断该用户数据为空
        if (loginEmployee == null) {
            return R.error("用户或密码错误");
        } else {
            //登录进来将用户信息存储session进行追踪
            session.setAttribute("employee", loginEmployee);
            return R.success(loginEmployee);
        }

    }
}
